# coding=utf-8
from . import admin
from flask import render_template, request, jsonify, make_response
from dbConnection.MysqlConn import Mysql
from app import util, PAGE_NUM, page_code
from app.util import login_required
import sys
import time, math

reload(sys)
sys.setdefaultencoding('utf8')


# 用户列表
@admin.route('/admin/user_list.html')
@login_required
def user_list():
    return render_template('admin/users/user_list.html')


# 获取用户列表数据
@admin.route('/admin/user_list_post', methods=['post'])
@login_required
def user_list_post():
    parms = ['uid']
    maps = {}
    for d in parms:
        maps[d] = request.values.get(d)
    p = request.values.get('p')
    c_num = (int(p) - 1) * PAGE_NUM
    sql = "select * FROM news_user WHERE 1=1"
    sql2 = "select count(*) con FROM news_user WHERE 1=1"
    if maps['uid']:
        sql += " and (uid = '%s' or  locate('%s', phone)>0)" % (maps['uid'], maps['uid'])
        sql2 += " and (uid = '%s' or  locate('%s', phone)>0)" % (maps['uid'], maps['uid'])
    sql += " order by registerTime desc limit %d, %d" % (c_num, PAGE_NUM)
    mysql = Mysql()
    res = mysql.getAll(sql, None)
    cont = mysql.getOne(sql2, None)
    mysql.dispose()
    pages = int(math.ceil(cont['con'] / float(PAGE_NUM))) if cont else 0
    route_path = 'get_data'
    html_str = page_code.page_code_html_post(int(p), route_path, pages) if pages > 0 else ''
    if res:
        for d in res:
            d['registerTime'] = str(d['registerTime'])
        data = {
            'status': 1,
            'res': res,
            'page': html_str,
            'p': p
        }
    else:
        data = {
            'status': 2,
            'page': html_str
        }
    return jsonify(data)


# 更新用户状态
@admin.route('/admin/update_user_status_post', methods=['post'])
@login_required
def update_user_status_post():
    uid = request.values.get('uid')
    status = request.values.get('status')
    mysql = Mysql()
    try:
        if status == '2':
            sql = "delete FROM news_user WHERE uid = '%s'" % uid
            res = mysql.delete(sql, None)
        else:
            sql = "update news_user SET status = '%s' WHERE uid = '%s'" % (status, uid)
            res = mysql.update(sql, None)
    except Exception as e:
        print e
    mysql.dispose()
    data = {}
    data['status'] = 0
    if res > 0:
        data['status'] = 1
    return jsonify(data)


# 用户详情
@admin.route('/admin/user_detail.html')
@login_required
def user_detail():
    uid = request.args.get('uid')
    sql = "select * FROM news_user WHERE uid = '%s'" % uid
    mysql = Mysql()
    res = mysql.getOne(sql, None)
    mysql.dispose()
    return render_template('admin/users/user_detail.html', res=res)


# 修改用户资料
@admin.route('/admin/update_user_detail_post', methods=['post'])
@login_required
def update_user_detail_post():
    uid = request.values.get('uid')
    points = request.values.get('points')
    sql = "update news_user SET points = '%s' WHERE uid = '%s'" % (points, uid)
    mysql = Mysql()
    res = mysql.update(sql, None)
    mysql.dispose()
    data = {}
    data['status'] = 0
    if res > 0:
        data['status'] = 1
    return jsonify(data)